/*获取订单号*/
var orderId = sessionStorage.getItem("orderId");
var platform=""
var lvtotal=0

getOrderDetail();

/*通过订单号获取订单详情*/
function getOrderDetail(){

    $.ajax({
        type: "POST",
        url: root.serviceProviderManagement.getServiceOrderDetail,
        cache: false, //禁用缓存
        dataType: "json",
        data: {
            "orderId": orderId, // 单据主键 
        },
        success: function(results) {
            var result=results.data
            /*转换状态*/
            var order_status = changeOrderState(result.orderStatus);
            $("#orderState").html('待审核');
            $("#dorderId").html(result.channelOrderId);   
            $("#orderId").html(result.orderId);
            var d = new Date(result.createdOrderAt);
            var atq = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
            console.log(atq)
            $("#creatOrderTime").html(result.createdOrderAt);
            $("#creatOrderTimes").html(result.pushOrderAt);
            $("cumterorderRemark").html(result.orderRemark)
            //基本信息 
            // if (result.platform == 10) {
            //     $("#platform").html("普通平台");
            // } else if (result.platform == 20) {
            //     $("#platform").html("签约平台");
            // }else if (result.platform == 30) {
            //     $("#platform").html("第三方平台");
            // }else if (result.platform == 40) {
            //     $("#platform").html("对接平台");
            // }
            $("#platform").html(result.platform );
            $("#customerName").html(result.parentCustomerName);
            $("#buyerName").html(result.buyerName);
            $("#providerName").html(result.providerName)
            $("#buyer-name").html(result.customerName);
            $("#created-order-code").html(result.orderAccount);
            $("#alisaAddress").html(result.projectName);
            $("#receiver-address").html(result.receiverProvinceName + "" + result.receiverCityName + "" + result.receiverAreaName + "" + result.receiverTownName + "" + result.receiverDetailAddress);
            $("#consignee").html(result.addressReceiverPOList[0].receiverName);
            $("#email").html(result.customerEmail);
            $("#cell-phone-number").html(result.addressReceiverPOList[0].mobile);
            $("#linePhone").html(result.customerLandlinePhone);
            $("#sales-manager").html(result.salesManager);
         
            $("#lng").html(result.lng+','+result.lat);
            $("#express_company2").html(result.expressCompany);
            $("#express_num").html(result.expressNum)
            $("#final_sent").html(changies(result.sentType))
            $("#delivery-method").html(changies(result.sentType))
      
            $("#pay-type").html(changePayTypeState(result.paidType));
            $("#invoice-type").html("个人(不开)")
            $("#yuncai_shipping_fee").html((result.yuncaiShippingFee).toFixed(2));
            $("#couponrealvalue").html((result.actualShippingFee).toFixed(2));
            $("#money-type").html(changePayTypeState(result.paidType));
            $("#channelShippingFee").html((result.channelShippingFee).toFixed(2));
            $("#dockShippingFee").html((result.dockShippingFee).toFixed(2));
            $("#orderCategory").html(result.orderType)
            $("#wanna_arrived_in").html(result.wannaArrivedBegin)
            $("#predixt-delivery-time").html(result.mayArrivedBegin);
            $("#peisong-time").html(SendGoodDatefn(result.sendGoodDate))
            $("#settlement-method").html(changePayTypeState(result.paidType));
            $("#settlement-amount").html(result.finalTotalAmount)
            $("#qualificationType").html(qualityFileRequiredfn(result.qualityFileRequired))
            $("#invoiceremark").html(result.warningRemark)
            $("#goodsAmount").html((result.goodsTotalAmount).toFixed(2))
            $("#fare").html((result.actualShippingFee).toFixed(2))
            $("#orderAmount").html((result.finalTotalAmount).toFixed(2))
            initprduct(result)
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}
//
function SendGoodDatefn(code){
    switch (code) {
        case 10:
            return "任何时间送货";
            break;
        case 20:
            return "工作日送货";
            break;
    }
}
/*转换状态函数*/
function changeOrderState(orderState) {
    switch(orderState) {
        /*订单状态*/
        case 9:
            return "待付款";
            break;
        case 10:
            return "待付款";
            break;
        case 20:
            return "待审核";
            break;
        case 30:
            return "待配货";
            break;
        case 40:
            return "待出库";
            break;
        case 50:
            return "待发货";
            break;
        case 60:
            return "待收货";
            break;
        case 70:
            return "已收货，待结算";
            break;
        case 80:
            return "订单成功";
            break;
        case 90:
            return "交易完成";
            break;
        case 100:
            return "已取消";
            break;
        case 110:
            return "交易关闭";
            break;
    }
}
 /*配送方式*/
function changies(secondarysenttype) {

    switch (secondarysenttype) {
        case 10:
            return "快递";
            break;
        case 40:
            return "鑫方盛物流";
            break;
    }
}
        /*支付方式*/
function changePayTypeState(payTypeState) {
    switch(payTypeState) {

        case 1001:
            return "在线支付";
            break;
        case 1002:
            return "支付宝支付";
            break;
        case 1003:
            return "微信支付";
            break;
        case 1101:
            return "货到付款";
            break;
        case 1102:
            return "货到付款-现金";
            break;
        case 1103:
            return "货到付款-pos机";
            break;
        case 1201:
            return "账期支付";
            break;
        case 1301:
            return "银行转账";
            break;
        case 1401:
            return "银行汇票";
            break;
    }
}
function  qualityFileRequiredfn(code){
    switch(code) {
        case 10:
        return "需要";
        break;
        case 20:
            return "不需要";
            break;
    }
}


/*预警备注编辑*/
var oldVal = "";

function toEdit() {
    oldVal = $(".orderRemark textarea").val();
    $("#isSaveFalse").css({
        display: "none"
    });
    $("#isSaveTrue").css({
        display: "block"
    });
    $(".orderRemark textarea").removeAttr("readonly");
}
function remarkToSave() {
    if ($(".orderRemark textarea").val().length > 200) {
        $(".orderRemark textarea").css({
            border: "1px red solid"
        });
        $(".errorMsg").css({
            display: "inline"
        });

    } else {
        $(".orderRemark textarea").css({
            border: "1px #ddd solid"
        });
        $(".errorMsg").css({
            display: "none"
        });
        var orderBaseId = JSON.parse(sessionStorage.getItem("orderBaseId"));
        var order_id = sessionStorage.getItem("orderId");
        var ourl = root.serviceProviderManagement.updateOrderWarn;
            var odata = {
                orderBaseId:orderBaseId,
                orderId: order_id,
                operatorId: JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.user_ID,
                operatorName: JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
                warningRemark: $("#comfirmRemark").val()
            }
        $.ajax({
            contentType: "application/json",
            type: "POST",
            url: ourl,
            cache: false, //禁用缓存
            dataType: "json",
            data: JSON.stringify(odata),
            success: function (result) {
                if (result.code == 0) {
                    location.replace(location.href);
                } else {
                    layer.msg(result.message, {icon: 5, time: 2000});
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }
        });
    }
}
function toCancle() {
    $(".orderRemark textarea").val(oldVal);
    $("#isSaveFalse").css({
        display: "block"
    });
    $("#isSaveTrue").css({
        display: "none"
    });
    $(".orderRemark textarea").css({
        border: "1px #ddd solid"
    });
    $(".errorMsg").css({
        display: "none"
    });
    $(".orderRemark textarea").attr("readonly", "readonly");
}
function initprduct(data){
var dataSource = new Array();
//table的数据源
var table; //table对象
dataSource = [];

/*商品信息*/

dataSource.splice(0, dataSource.length); //清空数组

if(data.itemResponseList!=null && data.itemResponseList!="" ){

    $.each(data.itemResponseList, function(i, field) {

        var rowData = new Array();

        rowData.push(field.productPic);
        rowData.push(field.productName);
        rowData.push(field.skuCode)
        rowData.push(field.skuInfo)
        rowData.push(field.sendGoodWarehouse)
        rowData.push(field.buyerCount);
        rowData.push(field.unitName)
        rowData.push(field.channelSettledPrice)
        rowData.push(field.settledPrice)
        rowData.push(field.purchasePrice)
        rowData.push(field.freightShare)
        rowData.push(field.productTotolPrice)
        dataSource.push(rowData);
    });

}
initDataTable(dataSource);
    function initDataTable(dataSource) {

        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({

            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": false,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [{
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function(data, type, row) {
                        if(row[12]==0){

                            var val = '<div style="cursor:pointer;"><img title="" style="float: left;width: 100px;height: 100px;" name="" src="'+row[0]+'" alt=""><div style="float: left;width:180px;padding-top: 20px;margin-left: 10px;"><div>' + row[1] + '</div><div>' + row[2] + '</div><div>' + row[3] + '</div></div></div>';
                        }else {

                            var val = '<div style="cursor:pointer;"><img  style="float: left;width: 100px;height: 100px;" name="" src="'+row[0]+'" alt=""><div style="float: left;width:180px;padding-top: 20px;margin-left: 10px;"><div>' + row[1] + '</div><div>' + row[2] + '</div><div>' + row[3] + '</div></div></div>';
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function(data, type, row) {
               
                        var val = '<span>' + row[4] + '</span>';
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function(data, type, row) {
                        var val = '<span>' + row[5] + '' + row[6] + '</span>';
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function(data, type, row) {
                        var val = '<span>' + (row[7]).toFixed(2) + '元</span>';
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function(data, type, row) {
                        var val = '<span>' + (row[8]).toFixed(2) + '元</span>';
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function(data, type, row) {
                        var val = '<span>' + (row[9]).toFixed(2) + '元</span>';
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function(data, type, row) {
                        var price = row[10].toFixed(2);
                        var val = '<span>' + price + '元</span>';
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function(data, type, row) {
                        var substotle=(row[11]).toFixed(2);
                        var val = '<span>' + substotle + '元'+'</span>';
                        return val;
                    },
                    "targets": 7

                }

            ]

        });

    };

}



















